package com.warrior.service;

import com.warrior.Exception.ProjectException;
import com.warrior.entity.ActivityEntity;
import com.warrior.entity.MatchDetailsEntity;
import com.warrior.respository.IMatchDetailsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * match service
 * Created by Shawn on 2017/3/30.
 */
@Service
public class MatchDetailsService {

    @Autowired
    private IMatchDetailsRepository matchDetailsRepository;

    @Autowired
    private ActivityService activityService;


    /**
     * 保存该赛事信息
     *
     * @param matchDetailsEntity 赛事对象
     * @return MatchDetailsEntity
     */
    @Transactional
    public MatchDetailsEntity saveNewMatchDetails(MatchDetailsEntity matchDetailsEntity) {
        this.matchDetailsRepository.save(matchDetailsEntity);
        return matchDetailsEntity;
    }

    /**
     * 根据该赛场id找到赛场信息
     * @param matchId 赛事详情id
     * @return MatchDetailsEntity
     */
    @Transactional
    public MatchDetailsEntity findMatchDetailById(String matchId) {
        MatchDetailsEntity matchDetailsEntity = this.matchDetailsRepository.findMatchDetailById(matchId);
        return matchDetailsEntity;
    }

    /**
     *
     * @param activityId 活动id
     * @return List<MatchDetailsEntity>
     * @throws ProjectException 未找到活动异常
     */
    @Transactional
    public List<MatchDetailsEntity> findMatchDetailsByActivityId(String activityId) throws ProjectException {
        ActivityEntity activityEntity = this.activityService.findActivityById(activityId);
        if (null != activityEntity) {
            List<MatchDetailsEntity> matchDetailsEntities = this.matchDetailsRepository.findMatchListByActivityByActiveId(activityEntity);
            return matchDetailsEntities;
        } else {
            throw new ProjectException("未找到该活动");
        }
    }

    @Transactional
    public void deleteMatchDetails(String matchId){
        this.matchDetailsRepository.delete(matchId);
    }


}
